package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.LogisticsStatusHistory;
import com.example.mall.dto.LogisticsStatusHistoryDto;
import com.example.mall.mapper.LogisticsStatusHistoryMapper;
import com.example.mall.query.LogisticsStatusHistoryQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.ILogisticsStatusHistoryService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 物流状态历史表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class LogisticsStatusHistoryServiceImpl extends ServiceImpl<LogisticsStatusHistoryMapper, LogisticsStatusHistory> implements ILogisticsStatusHistoryService {

    @Autowired
    private LogisticsStatusHistoryMapper logisticsStatusHistoryMapper;

    @Override
    public PageDTO<LogisticsStatusHistoryDto> queryPage(LogisticsStatusHistoryQuery logisticsStatusHistoryQuery) {
        if (logisticsStatusHistoryQuery == null) {
            logisticsStatusHistoryQuery = new LogisticsStatusHistoryQuery();
        }
        Page<LogisticsStatusHistory> page = logisticsStatusHistoryQuery.toMpPageByDefaultCreateTimeAsc();
        Page<LogisticsStatusHistory> cartPage = lambdaQuery()
                .like(logisticsStatusHistoryQuery.getHistoryId()!= null, LogisticsStatusHistory::getHistoryId, logisticsStatusHistoryQuery.getHistoryId())
                .like(logisticsStatusHistoryQuery.getLogisticsId()!= null, LogisticsStatusHistory::getLogisticsId, logisticsStatusHistoryQuery.getLogisticsId())
                .like(logisticsStatusHistoryQuery.getStatus()!= null, LogisticsStatusHistory::getStatus, logisticsStatusHistoryQuery.getStatus())
                .like(logisticsStatusHistoryQuery.getDescription()!= null, LogisticsStatusHistory::getDescription, logisticsStatusHistoryQuery.getDescription())
                .orderByDesc(LogisticsStatusHistory::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, LogisticsStatusHistoryDto.class);
    }

    @Override
    public void insert(LogisticsStatusHistoryDto logisticsStatusHistoryDto) {
        LogisticsStatusHistory logisticsStatusHistory = new LogisticsStatusHistory();
        CopyUtils.copyProperties(logisticsStatusHistoryDto, logisticsStatusHistory);
        logisticsStatusHistory.setHistoryId(Math.toIntExact(CodeGenerateUtils.generateUniqueId()));
        logisticsStatusHistory.setUpdateDate(new Date());
        logisticsStatusHistory.setStatus(String.valueOf(BaseConstants.EFFECTIVE_STATUS));
        logisticsStatusHistoryMapper.insert(logisticsStatusHistory);
    }

    @Override
    public void update(LogisticsStatusHistoryDto logisticsStatusHistoryDto) {
        AssertUtil.isNotNull(logisticsStatusHistoryDto.getHistoryId(), "物流状态历史ID不能为空");
        LogisticsStatusHistory logisticsStatusHistory = logisticsStatusHistoryMapper.selectById(logisticsStatusHistoryDto.getHistoryId());
        AssertUtil.isNotNull(logisticsStatusHistory, "物流状态历史不存在");
        CopyUtils.copyProperties(logisticsStatusHistoryDto, logisticsStatusHistory);
        logisticsStatusHistory.setUpdateDate(new Date());
        logisticsStatusHistoryMapper.updateById(logisticsStatusHistory);
    }

    @Override
    public void delete(Long historyId) {
        AssertUtil.isNotNull(historyId, "物流状态历史ID不能为空");
        LogisticsStatusHistory logisticsStatusHistory = logisticsStatusHistoryMapper.selectById(historyId);
        AssertUtil.isNotNull(logisticsStatusHistory, "物流状态历史不存在");
        logisticsStatusHistory.setStatus(String.valueOf(BaseConstants.LOSE_STATUS));
        logisticsStatusHistory.setUpdateDate(new Date());
        logisticsStatusHistoryMapper.updateById(logisticsStatusHistory);
    }

    @Override
    public void deleteBatch(List<Long> historyIds) {
        AssertUtil.isNotNull(historyIds, "物流状态历史ID列表不能为空");
        List<LogisticsStatusHistory> logisticsStatusHistories = logisticsStatusHistoryMapper.selectBatchIds(historyIds);
        AssertUtil.isNotNull(logisticsStatusHistories, "物流状态历史不存在");
        for (LogisticsStatusHistory logisticsStatusHistory : logisticsStatusHistories) {
            logisticsStatusHistory.setStatus(String.valueOf(BaseConstants.LOSE_STATUS));
            logisticsStatusHistory.setUpdateDate(new Date());
            logisticsStatusHistoryMapper.updateById(logisticsStatusHistory);
        }
    }

    @Override
    public LogisticsStatusHistory queryLogisticsStatusHistoryById(Long historyId) {
        AssertUtil.isNotNull(historyId, "物流状态历史ID不能为空");
        return logisticsStatusHistoryMapper.selectById(historyId);
    }

    @Override
    public List<LogisticsStatusHistory> queryLogisticsStatusHistoryList() {
        return this.list();
    }
}
